import { getToken, setToken, removeToken } from '@/utils/auth'
import user from '@/api/user'
const actions = {
  async login({ commit }, userInfo) {
    // todo 调用登录接口
    const token = await user.login(userInfo)
    commit('SET_TOKEN', token)
  },
  async getUserInfo({ commit }) {
    const data = await user.getUserInfo()
    commit('SET_USER_INFO', data)
  },
  logout({ commit }) {
    commit('REMOVE_TOKEN') // 删除token
    commit('SET_USER_INFO', {}) // 设置用户信息为空对象
  }

}
const mutations = {
  SET_TOKEN(state, token) {
    state.token = token
    setToken(token)
  },
  REMOVE_TOKEN(state, token) {
    state.token = null
    removeToken(token)
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo
  }
}
const state = {
  token: getToken(),
  userInfo: {}
}
export default {
  namespaced: true,
  state,
  actions,
  mutations
}
